
import requests
from pandas import DataFrame
import pandas as pd
from datetime import datetime




def get_day_klines_frame(code, start, end):
    """
    获取单只股票的详细数据
    """
    url = "https://53.push2his.eastmoney.com/api/qt/stock/kline/get"
    params = {
        "secid": f"1.{code}" if code.startswith("6") else f"0.{code}",
        "fields1": "f1,f2,f3,f4,f5,f6",
        "fields2": "f51,f52,f53,f54,f55,f56",
        "klt": "101",
        "fqt": "1",
        "beg": start,
        "end": end,
    }
    response = requests.get(url, params=params)
    body = response.json()
    if body['data'] is None:
        return "", []
    klines = body['data']['klines']
    data_list = []
    for kline in klines:
        print(kline)
        _date, _open, _close, _high, _low, _volume = kline.split(",")
        data_list.append([_date, float(_open), float(_close), float(_high), float(_low), int(_volume)])
    columns = ["Date", "Open", "Close", "High", "Low", "Volume"]
    data = DataFrame(data_list, columns=columns)
    # 计算5日均线
    data['SMA_5'] = data['Close'].rolling(window=5).mean()
    # 计算20日均线
    data['SMA_20'] = data['Close'].rolling(window=20).mean()
    # 计算30日均线
    data['SMA_30'] = data['Close'].rolling(window=30).mean()
    # 计算60日均线
    data['SMA_60'] = data['Close'].rolling(window=60).mean()
    # 计算120日均线
    data['SMA_120'] = data['Close'].rolling(window=120).mean()
    # 过去20日的最高价
    data['High_20_max'] = data['High'].rolling(window=20).max()
    # 过去20日的最大成交量
    data['Volume_20_max'] = data['Volume'].rolling(window=20).max()
    pd.DataFrame(data).to_csv("data.csv")
    return body['data']['name'], data


def get_stock_data(code, start, end):
    """
    获取股票的市值
    """
    url = "https://53.push2his.eastmoney.com/api/qt/stock/get"
    params = {
        "secid": f"1.{code}" if code.startswith("6") else f"0.{code}",
        "fields": "f57,f58,f117",
        "beg": start,
        "end": end,
    }
    response = requests.get(url, params=params)
    body = response.json()
    print(body)
    if body['data'] is None:
        return
    return {
        "code": body['data']['f57'],
        "name": body['data']['f58'],
        "market_cap": body['data']['f117'],
    }


params_shen_zheng = {
    "np": "1",
    "fltt": "1",
    "invt": "2",
    "cb": "jQuery37103398543358730397_1735711350110",
    "fs": "m:0+t:6,m:0+t:80",
    "fields": "f12,f13,f14,f1,f2,f4,f3,f152,f5,f6,f7,f15,f18,f16,f17,f10,f8,f9,f23",
    "fid": "f3",
    "pn": "1",
    "pz": "3000",
    "po": "1",
    "dect": "1",
    "ut": "fa5fd1943c7b386f172d6893dbfba10b",
    "wbp2u": "1277047353889980|0|1|0|web",
    "_": "1735711350216"
}


params_shang_zheng = {
    "np": "1",
    "fltt": "1",
    "invt": "2",
    "cb": "jQuery37103398543358730397_1735711350112",
    "fs": "m:1+t:2,m:1+t:23",
    "fields": "f12,f13,f14,f1,f2,f4,f3,f152,f5,f6,f7,f15,f18,f16,f17,f10,f8,f9,f23",
    "fid": "f3",
    "pn": "1",
    "pz": "2000",
    "po": "1",
    "dect": "1",
    "ut": "fa5fd1943c7b386f172d6893dbfba10b",
    "wbp2u": "1277047353889980|0|1|0|web",
    "_": "1735711350115"
}

params_chuangye = {
    "np": "1",
    "fltt": "1",
    "invt": "2",
    "cb": "jQuery37103398543358730397_1735711350110",
    "fs": "m:0+t:80",
    "fields": "f12,f13,f14,f1,f2,f4,f3,f152,f5,f6,f7,f15,f18,f16,f17,f10,f8,f9,f23",
    "fid": "f3",
    "pn": "1",
    "pz": "20",
    "po": "1",
    "dect": "1",
    "ut": "fa5fd1943c7b386f172d6893dbfba10b",
    "wbp2u": "1277047353889980|0|1|0|web",
    "_": "1735711350240"
}

def get_all_ticket(params):
    url = "https://push2.eastmoney.com/api/qt/clist/get?"

    # 发送HTTP请求
    response = requests.get(url, params=params)

    # 检查响应状态码
    if response.status_code == 200:
        # 获取响应文本
        text = response.text
        
        # 去除回调函数名
        start_index = text.find('(') + 1
        end_index = text.rfind(')')
        json_str = text[start_index:end_index]
        
        # 解析JSON数据
        data = eval(json_str)
        
        # 提取股票信息
        stocks = data['data']['diff']
        
        # 字段映射
        field_mapping = {
            "f2": "最新价",          # / 100
            "f3": "涨跌幅",         # / 10000
            "f4": "涨跌额",         # / 100
            "f5": "成交量(手)",   # / 10000
            "f6": "成交额",         # / 100000000
            "f7": "振幅",           # / 10000
            "f8": "换手率",         # / 10000
            "f9": "市盈率(动态)",  # / 100
            "f10": "量比",          # / 100
            "f12": "代码",
            "f14": "名称",
            "f15": "最高",           # / 100
            "f16": "最低",           # / 100
            "f17": "今开",           # / 100
            "f18": "昨收",           # / 100
            "f23": "市净率",         # / 100
        }
        
        # 打印每个股票的信息
        all_data = []
        for stock in stocks:
            stock_info = {}
            for key, value in stock.items():
                try:
                    if key in field_mapping:
                        chinese_key = field_mapping[key]
                        if key in ["f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f15", "f16", "f17", "f18", "f23"]:
                            if key in ["f2", "f15", "f16", "f17", "f18", "f10"]:
                                stock_info[chinese_key] = value / 100
                            elif key in ["f3", "f7", "f8", "f9", "f23"]:
                                stock_info[chinese_key] = value / 10000
                            elif key == "f4":
                                stock_info[chinese_key] = value / 100
                            elif key == "f5":
                                stock_info[chinese_key] = value / 10000
                            elif key == "f6":
                                stock_info[chinese_key] = value / 100000000
                        else:
                            stock_info[chinese_key] = value
                except:
                    pass
            all_data.append(stock_info)
        return all_data
    else:
        print(f"Failed to retrieve data. Status code: {response.status_code}")


def get_three_shichang():
    shang_zheng = get_all_ticket(params_shang_zheng)
    # shang_zheng = pd.DataFrame(res).to_excel("./dataset/data/all_ticket_shangzheng.xlsx",  index=False)
    shen_zheng = get_all_ticket(params_shen_zheng)
    # shen_zheng = pd.DataFrame(res).to_excel("./dataset/data/all_ticket_shenzheng.xlsx",  index=False)
    chuangye = get_all_ticket(params_chuangye)
    # chuangye = pd.DataFrame(res).to_excel("./dataset/data/all_ticket_shenzheng.xlsx",  index=False)

    return shang_zheng + shang_zheng + chuangye




def get_shichang(size):

 
    # API URL
    url = f'https://datacenter-web.eastmoney.com/api/data/v1/get?reportName=RPTA_RZRQ_LSHJ&columns=ALL&source=WEB&sortColumns=DIM_DATE&sortTypes=-1&pageNumber=1&pageSize={size}&filter=&callback=jQuery112309696695504811084_1735910018177&_=1735910018179'

    try:
        # 发送GET请求
        response = requests.get(url)
        response.raise_for_status()  # 检查请求是否成功

        # 获取响应内容
        data = response.text

        # 移除JSONP回调函数包裹
        # 提取JSON部分
        json_str = re.search(r'\({(.*)}\);', data).group(1)
                #    re.search(r'\(({.*})\);', data).group(1)
        # 将字符串转换为JSON对象
        json_data = json.loads("{" + json_str + "}")
        # 提取数据部分
        diff_data = json_data['result']['data']
        # 定义需要的列及其对应的字段名
        columns_mapping = {
            "DIM_DATE": "交易日期",
            "NEW": "收盘-沪深300",
            "ZDF": "涨跌幅-沪深300",
            "RZYE": "融资余额",
            "RZYEZB": "融资余额占比",
            "RZMRE": "融资买入额",
            "RZCHE": "融资偿还额",
            "RZJME": "融资净买入",
            "RQYE": "融券余额",
            "RQYL": "融券余量",
            "RQMCL": "融券卖出量",
            "RQCHL": "融券偿还量",
            "RQJMG": "融券净卖出",
            "RZRQYE": "融资融券余额",
            "RZRQYECZ": "融资融券余额差值"
        }

        # 提取所需字段并重命名
        # extracted_data = []
        for item in diff_data:
            extracted_item = {}
            # extracted_item = {columns_mapping[key]: value for key, value in item.items() if key in columns_mapping}
            for key, value in item.items():
                if key in columns_mapping:
                    if key in ['RZYE', "RZMRE", "RZCHE", "RZJME", "RQYE", "RQYL", "RQCHL", "RQMCL", "RQJMG", "RZRQYE", "RZRQYECZ"]:
                        value = value / 100000000
                    extracted_item[columns_mapping[key]] = value
            # extracted_data.append(extracted_item)
            # insert_stock_market(extracted_item)
        # # 将数据转换为DataFrame
        # df = pd.DataFrame(extracted_data)
        # # 显示前几行数据
        # df.to_excel("aa.xlsx")

    except requests.exceptions.RequestException as e:
        print(f"Error fetching data: {e}")



if __name__ == "__main__":
    # print(get_stock_data("603636",  "20241227", "20241228"))
    # print(get_day_klines_frame("603636", "20241227", "20241227"))
    # res = get_all_ticket(params_shang_zheng)
    # pd.DataFrame(res).to_excel("./dataset/data/all_ticket_shangzheng.xlsx",  index=False)
    # res = get_all_ticket(params_shen_zheng)
    # pd.DataFrame(res).to_excel("./dataset/data/all_ticket_shenzheng.xlsx",  index=False)



    # 1、 获取所有股票、 通过基础指标选取 合适的个股
    # 2、 获取第一步选取的个股， 判断他的市值， 市值在 500 - 1000 亿 之间的
    # 3、 获取第二部所筛选的个股， 获取他详细的数据， 包括均值， 找 5日线  10日线 20日线 的顺序判断个股
    # 4、 找到个股对应的时间点， 用历史数据训练模型， 判断未来5天， 股价的 预测值， 计算准确性


    get_shichang()

